package parser

import (
	"gitee.com/jnshao/go-start/projects/crawler/model"
	"regexp"
	"strconv"
)

var basicRe = regexp.MustCompile(`{"age":([0-9]+),"avatarURL":"([^"]+)","car":"([^"]+)","children":"([^"]+)","constellation":"([^"]+)","education":"([^"]+)","height":([0-9]+),"house":"([^"]+)","introduceContent":"([^"]+)","isRecommend":([^"]+),"lastModTime":"([^"]+)","marriage":"([^"]+)","memberID":([0-9]+),"nickName":"([^"]+)","objectAge":"([^"]+)","objectHight":"([^"]+)","objectMarriage":"([^"]+)","objectSalary":"([^"]+)","occupation":"([^"]+)","salary":"([^"]+)","sex":([0-9]+),"workCity":"([^"]+)"}`)

func PraseProfile(content []byte, username string) model.Profile {
	profile := model.Profile{Name: username}
	basicmatch := basicRe.FindAllSubmatch(content, -1)
	for _, basicsubmatch := range basicmatch {
		if string(basicsubmatch[14]) != username {
			continue
		}
		profile.Marriage = string(basicsubmatch[12])
		age, err := strconv.Atoi(string(basicsubmatch[1]))
		if err == nil {
			profile.Age = age
		}
		profile.Xinzuo = string(basicsubmatch[5])
		height, err := strconv.Atoi(string(basicsubmatch[7]))
		if err == nil {
			profile.Height = height
		}
		profile.Occupation = string(basicsubmatch[19])
		profile.Income = string(basicsubmatch[20])
		profile.Education = string(basicsubmatch[6])

		profile.Hokou = string(basicsubmatch[22])
		profile.House = string(basicsubmatch[8])
		profile.Car = string(basicsubmatch[3])
		profile.Gender = func(gender []byte) string {
			switch string(gender) {
			case "0":
				return "男士"
			default:
				return "女士"
			}
		}(basicsubmatch[21])
	}
	return profile
}
